{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import math\n",
    "import numpy as np\n",
    "import nibabel as nib\n",
    "from k3d.helpers import download\n",
    "import ipywidgets as widgets\n",
    "import vtk\n",
    "from vtk.util import numpy_support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "basic_color_maps = [(attr, getattr(k3d.basic_color_maps, attr)) for attr in dir(k3d.basic_color_maps) if not attr.startswith('__')]\n",
    "paraview_color_maps = [(attr, getattr(k3d.paraview_color_maps, attr)) for attr in dir(k3d.paraview_color_maps) if not attr.startswith('__')]\n",
    "matplotlib_color_maps = [(attr, getattr(k3d.matplotlib_color_maps, attr)) for attr in dir(k3d.matplotlib_color_maps) if not attr.startswith('__')]\n",
    "colormaps = basic_color_maps + paraview_color_maps + matplotlib_color_maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = download('https://vedo.embl.es/examples/data/embryo.slc')\n",
    "reader = vtk.vtkSLCReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "vti = reader.GetOutput()\n",
    "\n",
    "bounds = vti.GetBounds()\n",
    "x, y, z = vti.GetDimensions()\n",
    "img = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "tf_editor = k3d.transfer_function_editor()\n",
    "volume = k3d.volume(img.astype(np.float16))\n",
    "\n",
    "@widgets.interact(x=widgets.Dropdown(options=colormaps, description='ColorMap:'))\n",
    "def g(x):\n",
    "    tf_editor.color_map = np.array(x, dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "plot = k3d.plot()\n",
    "plot += volume\n",
    "tf_editor.display()\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = widgets.jslink((tf_editor, 'color_map'), (volume, 'color_map'))\n",
    "a = widgets.jslink((tf_editor, 'opacity_function'), (volume, 'opacity_function'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}